from fastapi import FastAPI, File, UploadFile
from main import my_paddleocr_img
from main_pdf import my_paddleocr_pdf
import os
import uuid

app = FastAPI()


@app.get("/")
async def root():
    return {"message": "Hello paddleocr"}


@app.post("/uploadfile/")
async def create_upload_file(file: UploadFile):
    file_content = await file.read()
    result = my_paddleocr_img(file_content)
    return {"filename": file.filename, "content_type": file.content_type, "result": result}


@app.post("/uploadfilepdf/")
async def create_upload_file(file: UploadFile):
    tmp_pdf_path = "tmp-{}.pdf".format(uuid.uuid4())
    file_content = await file.read()
    with open(tmp_pdf_path, "wb") as f:
        f.write(file_content)
    result = my_paddleocr_pdf(tmp_pdf_path)
    os.remove(tmp_pdf_path)
    return {"filename": file.filename, "content_type": file.content_type, "result": result}
